var router = require("./router"),
    url = require("url"),
    qs = require("querystring"),
    Channel = require("./channel").Channel;
    msgCtrl = require('./admin').msgCtrl;
function Server() {
    this.httpServer = router.createServer();
    this.channels = [];
}
extend(Server.prototype, {
    listen: function(port, host) {
        this.httpServer.listen(port, host);
    },
    passThru: function(path, handler) {
        this.httpServer.get(path, handler);
    },
    addChannel: function(options) {
        var httpServer = this.httpServer,
            channel = new Channel(options);
        if (!channel) {
            return false;
        }
        this.channels.push(channel);
        handlers.forEach(function(handler) {
            httpServer.get(channel.basePath + handler.path,
                handler.handler.partial(channel));
        });
        return channel;
    }
});
exports.createServer = function() {
    return new Server();
};
 
var handlers = [
    { path: "/who", handler: function(channel, request, response) {
        /*var nicks = [];
        for (var id in channel.sessions) {
            nicks.push(channel.sessions[id].nick);
        }
        response.simpleJSON(200, { nicks: nicks });*/
        response.simpleJSON(200, { nicks: [] });
    } },
    { path: "/join", handler: function(channel, request, response) {
/*
        var nick = qs.parse(url.parse(request.url).query).nick;
        if (!nick) {
            response.simpleJSON(400, { error: "bad nick." });
            return;
        }
*/
        var verify = {};
        try{
            verify.ip = request.connection.remoteAddress;
        }catch(e){
            console.log('ip connot found');
        }
        var session = channel.createSession(verify);
        if (!session) {
            response.simpleJSON(400, { error: "unknow" });
            return;
        }
        if (!msgCtrl.checkJoin(session)){
            response.simpleJSON(500, { errorCode: 500, error: "TLE."} );
            return;
        }
        response.simpleJSON(200, { id: session.id, nick: session.nick, since: session.since });
    } },
    { path: "/setNick", handler: function(channel, request, response) {
        var query = qs.parse(encodeURI(url.parse(request.url).query)),
            nick = query.nick,
            session = channel.sessions[query.id];
        if (!session || !nick || !session.setNick(nick) ) {
            response.simpleJSON(400, { error: "bad nick." });
            return;
        }else{
            response.simpleJSON(200, { id: session.id, nick: session.nick, since: session.since });
        }
    } },
    { path: "/part", handler: function(channel, request, response) {
        var id = qs.parse(encodeURI(url.parse(request.url).query)).id;
        var eventId = channel.destroySession(id);
        response.simpleJSON(200, { id: eventId });
    } },
    { path: "/recv", handler: function(channel, request, response) {
        var query = qs.parse(encodeURI(url.parse(request.url).query)),
            since = parseInt(query.since, 10),
            session = channel.sessions[query.id];
        if (!session) {
            response.simpleJSON(400, { error: "No such session id." });
            return;
        }
        if (isNaN(since)) {
            response.simpleJSON(400, { error: "Must supply since parameter." });
            return;
        }
        //verify
        if (!msgCtrl.checkRecv(session)){
            response.simpleJSON(200, { errorCode: 400, error: "System Error."} );
            return;
        }
        session.poke();
        channel.query(since, function(messages) {
            session.poke();
            response.simpleJSON(200, { messages: messages });
        });
    } },
    { path: "/send", handler: function(channel, request, response) {
        //console.log(request.url,url.parse(request.url).query);
        var query = qs.parse(url.parse(request.url).query),
			text = query.text;
            session = channel.sessions[encodeURI(query.id)];
        //console.log(query);
		if (!session) {
			response.simpleJSON(400, { error: "No such session id." });
			return;
		}
        //verify
        text = msgCtrl.checkSend(text, session);
        if (!text){
            response.simpleJSON(200, { errorCode: 404, error: "Runtime Error."} );
            return;
        }
        text = encodeURI(text);
		if (!text || !text.length) {
			response.simpleJSON(400, { error: "Must supply text parameter." });
			return;
		}
        session.poke();
        msgCtrl.newMessage(text, session);
        
        if (session.preCheck(text)){
            session.setRate();
            //text = encodeURIComponent(text);
            var id = channel.appendMessage(session.nick, "msg", text);
            response.simpleJSON(200, { id: id });
        }else{
            response.simpleJSON(200, { errorCode: 403, error: "Repeated message." });
			return;
        }
    } }
];
var slice = [].slice;
Function.prototype.partial = function() {
    var fn = this,
        args = slice.call(arguments);
    return function() {
        return fn.apply(this, args.concat(slice.call(arguments)));
    };
};
function extend(obj, props) {
    for (var prop in props) {
        obj[prop] = props[prop];
    }
}
